package pri.zey.custom.AFMananger;/**
 * Created by Administrator on 2016/4/14 0014.
 */

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.lang.ref.SoftReference;
import java.util.Stack;

import pri.zey.custom.tools.Tkit;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * Activity基础操作层 主要：完成基础逻辑操作，提供给AFManager方便操作
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 *
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 * 增删改查
 * 获取第一个activity
 * 清空堆栈
 *
 */
public class ActivityControl extends AbstractBase<ActivityBase> {

    private Stack<SoftReference<ActivityBase>> stack = new Stack<SoftReference<ActivityBase>>();

    private boolean isResult = false;
    private final int NumState = -1;

    @Override
    public void add(ActivityBase mActivityBase) {
        add(mActivityBase.getName(), mActivityBase);
    }

    @Override
    public void add(String tag, ActivityBase mActivityBase) {
        stack.push(new SoftReference<ActivityBase>(mActivityBase));
    }

    @Override
    public void del(String tag) {
        del(tag, null, NumState);
    }

    @Override
    public void del(ActivityBase mActivityBase) {
        del(null, mActivityBase, NumState);
    }

    @Override
    public void del(int index) {
        del(null, null, index);
    }

    @Override
    public void del(String tag, ActivityBase mActivityBase, int index) {
        if(stack.size() > 0) {
            for (int i = stack.size() - 1; i >= 0; i--){
                SoftReference<ActivityBase> sr = stack.get(i);
                if (sr.get().getName().equals(tag)
//                        || sr.get().getName().equals(mActivityBase == null ? null : mActivityBase.getName())
                        || sr.get().getName().equals(isNull(mActivityBase) ? null : mActivityBase.getName())
                        || index > 0 && index < stack.size() && stack.search(sr) == index) {
                    sr.get().finish();
                    stack.remove(sr);
                    break;
                }
            }
        }
    }

    @Override
    public void delAll() {
        for (SoftReference<ActivityBase> sr : stack)
            sr.get().finish();
        stack.clear();
    }

    @Override
    public void update(ActivityBase old, ActivityBase news) {
        update(NumState, getT(old), news);
    }

    @Override
    public void update(int index, ActivityBase news) {
        update(index, null, news);
    }

    /**
     * 把旧的删除，启动新的activity到顶部显示
     * @param index
     * @param old
     * @param news
     */
    @Override
    public void update(int index, ActivityBase old, ActivityBase news) {
        if(news != null)
            if(index != NumState) {
                stack.get(index).get().startActivity(new Intent(old.getApplicationContext(), news.getClass()));
                del(stack.get(index).get());
            }else if(old != null){
                old.startActivity(new Intent(old.getApplicationContext(), news.getClass()));
                del(old);
            }
    }

    @Override
    public boolean isNull(String tag) {
        return isNull(tag, null);
    }

    @Override
    public boolean isNull(ActivityBase mActivityBase) {
        return isNull(null, mActivityBase);
    }

    @Override
    public boolean isNull(String tag, ActivityBase mActivityBase) {
        if(getIndex(tag, mActivityBase) != NumState)
            return false;
        else
            return true;
    }

    @Override
    public int getIndex(String tag) {
        return getIndex(tag, null);
    }

    @Override
    public int getIndex(ActivityBase mActivityBase) {
        return getIndex(null, mActivityBase);
    }

    @Override
    public int getIndex(String tag, ActivityBase mActivityBase) {
        for (int i = stack.size() - 1; i > 0; i--){
            SoftReference<ActivityBase> sr = stack.get(i);
            if(Tkit.notBlank(tag) && sr.get().getName().equals(tag)
                    || mActivityBase != null && sr.get().getName().equals(mActivityBase.getName())) {
                return i;
            }
        }
        return NumState;
    }

    @Override
    public ActivityBase getT(String tag) {
        return getT(tag, null);
    }

    @Override
    public ActivityBase getT(ActivityBase mActivityBase) {
        return getT(null, mActivityBase);
    }

    @Override
    public ActivityBase getT(String tag, ActivityBase mActivityBase) {
        int index = getIndex(tag, mActivityBase);
        if(index == NumState)
            return null;
        SoftReference<ActivityBase> sr = stack.get(index);
        return sr.get();
    }

    @Override
    public ActivityBase getTop() {
        return stack.peek().get();
    }

    @Override
    public void sendMsg(Handler handler, InterfaceHandler IHandler) {
        sendMsg(getTop().getName(), null, handler, IHandler);
    }

    @Override
    public void sendMsg(String tag,Handler handler, InterfaceHandler IHandler) {
        sendMsg(tag, null, handler, IHandler);
    }

    @Override
    public void sendMsg(ActivityBase mActivityBase,Handler handler, InterfaceHandler IHandler) {
        sendMsg(null, mActivityBase, handler, IHandler);
    }

    @Override
    public void sendMsg(String tag, ActivityBase mActivityBase, Handler handler, InterfaceHandler IHandler) {
        ActivityBase ab = getT(tag, mActivityBase);//获得指定activity
        if(ab != null){
            Bundle bundle = new Bundle();
            Message msg = handler.obtainMessage();
            msg.what = ab.SMsgCode;
            msg.obj = ab;
            msg.setData(bundle);
            ab.IHandler = IHandler;
            handler.sendMessage(msg);//启动顶部activity send
        }
    }

    @Override
    public void SMsg(String tag, InterfaceHandler IHandler) {
        ActivityBase ab = getT(tag);//获得指定activity
        if(ab != null){
            Bundle bundle = new Bundle();
            Message msg = new Message();
            msg.what = 110;
            msg.obj = ab;
            Log.i("TAGSS", "getTop : " + getTop());
            getTop().MsgHandler.sendMessage(msg);
        }
    }
}
